package com.wongoing.aop;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSON;
import com.wongoing.annotation.SameUrlData;
import com.wongoing.util.LogService;

/**
 * 功能说明：SpringMVC 重复请求拦截器
 * 修改说明：
 * @author zhenglibing
 * @date 2017年10月31日 下午4:46:23
 * @version 0.1
 */
public class SameUrlDataInterceptor extends HandlerInterceptorAdapter {
	/**
	 * 开始处理请求
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (handler instanceof HandlerMethod) {
			HandlerMethod handlerMethod = (HandlerMethod)handler;
			Method method = handlerMethod.getMethod();
			SameUrlData annotation = method.getAnnotation(SameUrlData.class);
			if (annotation != null) {
				if (this.repeatDataValidator(request)) { //如果为重复提交
					return false;
				}
				else {
					return true;
				}
			}
			return true;
		}
		else {
			return super.preHandle(request, response, handler);
		}
	}
	
	/**
	 * 功能说明：验证数据是否重复提交
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017年10月31日 下午4:45:42
	 * @param request
	 * @return
	 */
	private boolean repeatDataValidator(HttpServletRequest request) {
		try {
			if (null == request.getParameterMap()) {
				System.out.println("repeatDataValidator request is null.");
				return true;
			}
			else
			{
				System.out.println("repeatDataValidator request is not null.");
			}
			String params = JSON.toJSONString(request.getParameterMap());
			System.out.println("---------------------repeatDataValidator begin---------------------");
			System.out.println(params);
			System.out.println("---------------------repeatDataValidator end-----------------------");
			if (params.equals("{}")) {
				return true;
			}
			String url = request.getRequestURI();
			Map<String, String> map = new HashMap<String, String>();
			map.put(url, params);
			String nowUrlParams = map.toString();
			Object preUrlParams = request.getSession(false).getAttribute("repeatData");
			if (preUrlParams == null) {		//如果上一个数据为null，标识还没有访问页面
				request.getSession().setAttribute("repeatData", nowUrlParams);
				return false;
			}
			else {		//否则已经访问过页面
				if (preUrlParams.toString().equals(nowUrlParams)) {	//如果上次url+数据和本次url+数据相同，则标识重复提交
					return true;
				}
				else { //如果上次url+数据和本次url+数据不同，则不是重复提交
					request.getSession(false).setAttribute("repeatData", nowUrlParams);
					return false;
				}
			}
		}
		catch(Exception ex) {
			LogService.getInstance(this).error(ex.getMessage());
			return true;
		}
	}
	
	/**
	 * 请求处理完毕，页面呈现之前
	 */
//	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception {
//		
//	}
	
	/**
	 * 页面呈现后指定的回调
	 */
//	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
//			throws Exception {
//		
//	}
}
